﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Service.Interface;
using Clannan.Models.JsonModel;
using Domain;

namespace Clannan.Controllers
{
    public class MobileImageController : Controller
    {
        public IMobileImageService MobileImageService;

        /// <summary>
        /// 获取手机图片分页集合
        /// </summary>
        /// <param name="pageIndex">页索引</param>
        /// <param name="pageSize">页大小</param>
        /// <returns></returns>
        public JsonResult GetByPage(string pageIndex, string pageSize)
        {
            List<MobileImageJsonModel> mobileimageList = new List<MobileImageJsonModel>();//Json返回页集合
            MobileImageJsonModel mobileimageJsonModel; //Json模型
            int pageCount;               //总页数

            //获取分页数据
            IList<MobileImage> mobilepageListData = this.MobileImageService
                .LoadAllMobileImageByPage(Convert.ToInt32(User.Identity.Name), Convert.ToInt32(pageIndex), Convert.ToInt32(pageSize), out pageCount, false);
            //数据处理
            foreach (MobileImage mobileimage in mobilepageListData)
            {
                mobileimageJsonModel = new MobileImageJsonModel();
                mobileimageJsonModel = mobileimageJsonModel.Copy(mobileimageJsonModel, mobileimage);
                mobileimageList.Add(mobileimageJsonModel);
            }

            return Json(new { Message = 1, MobileImageList = mobileimageList, PageCount = pageCount });
        }

        /// <summary>
        /// 获取手机图片页
        /// </summary>
        /// <param name="memoID">手机图片页ID</param>
        /// <returns></returns>
        public JsonResult Get(string mobileimageID)
        {
            MobileImage mobileimage = this.MobileImageService.Get(mobileimageID);
            var result = new
            {
                ID = mobileimage.ID,
                Content = mobileimage.Content,
                CreateTime = mobileimage.CreateTime,
                ImageID = mobileimage.Image.ID,
                ImageName = mobileimage.Image.Name,
                ImagePath = mobileimage.Image.Path
            };

            return Json(result);
        }

        public void Create()
        {

        }

        /// <summary>
        /// 将手机图片页放入垃圾箱
        /// </summary>
        /// <param name="mobileimageID">手机图片页ID</param>
        /// <returns></returns>
        public JsonResult Throw(string mobileimageID)
        {
            this.MobileImageService.UpdateMobileImageMarkDelete(mobileimageID, true);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 从垃圾箱中还原手机图片页
        /// </summary>
        /// <param name="mobileimageID">手机图片页ID</param>
        /// <returns></returns>
        public JsonResult Restore(string mobileimageID)
        {
            this.MobileImageService.UpdateMobileImageMarkDelete(mobileimageID, false);

            return Json(new { Message = 1 });
        }

        /// <summary>
        /// 删除手机图片页
        /// </summary>
        /// <param name="mobileimageID">手机图片页ID</param>
        /// <returns></returns>
        public JsonResult Delete(string mobileimageID)
        {
            this.MobileImageService.Delete(mobileimageID);

            return Json(new { Message = 1 });
        }

    }
}
